package top.smallway.bs_vr.requestsBS;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;

import androidx.appcompat.app.AlertDialog;

import top.smallway.bs_vr.IDownloadlister;

public class DownloadUtils {
    private DownloadManager downloadManager = null;
    private long myDwonloadID;
    private AlertDialog dialog;
    private String url;
    private String fileName;

    public void download() {
        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        //创建下载任务，url即任务链接
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        //指定下载路径及文件名
        request.setDestinationInExternalPublicDir("/Download/BS曲包/", fileName+".zip");
        //获取下载管理器
        downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        //一些配置
        //允许移动网络与WIFI下载
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        //是否在通知栏显示下载进度
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        //将任务加入下载队列
        assert downloadManager != null;
        final long id = downloadManager.enqueue(request);
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //获取下载id
                myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
                if (myDwonloadID == id) {
                    //获取下载uri
                    Uri uri = downloadManager.getUriForDownloadedFile(myDwonloadID);
                    lister.success(uri,dialog);
                }
            }
        };
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            activity.registerReceiver(receiver, filter);
        }
    }
    public DownloadUtils showProgressDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("正在下载歌曲" + fileName);
        builder.setMessage(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);



        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                downloadManager.remove(myDwonloadID);
            }
        });
        dialog = builder.create();
        dialog.show();
        return this;
    }

    private IDownloadlister lister = null;
    //Context
    private Context context;

    public static DownloadUtils builder() {
        return new DownloadUtils();
    }

    public DownloadUtils setUrl(String url) {
        this.url = url;
        return this;
    }

    public DownloadUtils setLister(IDownloadlister lister) {
        this.lister = lister;
        return this;
    }


    public DownloadUtils setFileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    public DownloadUtils setContext(Context context) {
        this.context = context;
        return this;
    }
}